﻿using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using LunchLearn.WebApi.Domain;

namespace LunchLearn.WebApi.Repository
{
    public class LunchLearnContext : DbContext
    {

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Order>().HasKey(x => x.Id);
            modelBuilder.Entity<Order>().HasMany(x => x.OrderLines)
                .WithRequired(x => x.Order).HasForeignKey(x => x.OrderId).WillCascadeOnDelete();
            modelBuilder.Entity<Order>().HasRequired(x => x.Customer).WithMany().HasForeignKey(x => x.CustomerId).WillCascadeOnDelete(false); ;
            modelBuilder.Entity<Order>().Ignore(x => x.TotalAmount);

            modelBuilder.Entity<OrderLine>().HasKey(x => x.Id);
            modelBuilder.Entity<OrderLine>().HasRequired(x => x.Item).WithMany().HasForeignKey(x => x.ItemId).WillCascadeOnDelete(false);
            modelBuilder.Entity<OrderLine>().Ignore(x => x.TotalAmount);

            modelBuilder.Entity<User>().HasKey(x => x.Id);
            modelBuilder.Entity<User>().HasRequired(x => x.Role).WithMany().HasForeignKey(x => x.RoleId).WillCascadeOnDelete(false);

            modelBuilder.Entity<Role>().HasKey(x => x.Id);

            modelBuilder.Entity<Customer>().HasKey(x => x.Id);
        }

        public DbSet<Order> Orders { get; set; }
        public DbSet<OrderLine> OrderLines { get; set; }
        public DbSet<Item> Items { get; set; }
        public DbSet<Customer> Customers { get; set; }
        public DbSet<User> Users { get; set; }
        public DbSet<Role> Roles { get; set; }


    }
}